[PR]

水無瀬の部屋 > Programming > sample > tools > misc > sha1.cpp
最終更新日: 2007/03/24

   1: //*********************************************************
   2: // プロジェクト: TOOLS::SHA1 - Secure Hash Algorithm
   3: //   ファイル名: sha1.cpp
   4: //*********************************************************
   5: #include <misc/sha1.h>       //
   6: #include <header/tooldbg.h>  // ASSERT(), 
   7: #include <header/toolbase.h> // 
   8: #include <header/snprintf.h> // snprintf(), vsnprintf(), 
   9: #include <limits.h>          // CHAR_BIT, 
  10: 
  11: 
  12: //---------------------------------------------------------
  13: // テスト関数 の 宣言
  14: //---------------------------------------------------------
  15: DECLARE_TESTPROC( test_sha1 );
  16: 
  17: 
  18: //---------------------------------------------------------
  19: // 定数型マクロ の 定義
  20: //---------------------------------------------------------
  21: #define DBG_ALLOCNAME  "SHA1_CreateAlgorithm"
  22: 
  23: 
  24: //---------------------------------------------------------
  25: // 構造体 の 宣言
  26: //---------------------------------------------------------
  27: typedef struct SHA1_CTX_tag
  28: {
  29: //	DWORD count;
  30: 	DWORD index;        // バッファの最終書込バイト位置
  31: 	BYTE  buffer[ 64 ];
  32: 
  33: 
  34: 	// 総書込バイト数
  35: 	// [1 = l] xxxxxxxxxxxxxxxxxxxxxxxxxxxxx000 , 総バイト数の下位 29 bits
  36: 	// [0 = h] 00000000000000000000000000000xxx , 総バイト数の上位  3 bits
  37: 	DWORD length[ 2 ];
  38: 	DWORD state[ 5 ];
  39: } SHA1_CTX;
  40: 
  41: 
  42: //---------------------------------------------------------
  43: // ファイルスコープ関数 の 宣言
  44: //---------------------------------------------------------
  45: static void byte_reverse( DWORD *buffer, int count );
  46: static void add_count( SHA1_CTX *context, int nread );
  47: static void SHA1_Transform( SHA1_CTX *context, const BYTE *src, int length );
  48: static bool SHA1_IsValidContext( const SHA1_CTX *context );
  49: 
  50: 
  51: //*********************************************************
  52: // SHA1_CreateAlgorithm
  53: //*********************************************************
  54: SHA1_CTX *
  55: SHA1_CreateAlgorithm
  56: 	(
  57: 		void
  58: 	)
  59: {
  60: 	CALLONCE_TESTPROC( test_sha1 ); // [テスト]
  61: 
  62: 	SHA1_CTX *context = (SHA1_CTX *)malloc( sizeof( *context ) );
  63: 	if ( !context )
  64: 		return null;
  65: 
  66: 	SHA1_InitData( context );
  67: 	ASSERT( SHA1_IsValidContext( context ) );
  68: 	
  69: 	DBG_LOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
  70: 	return context;
  71: }//SHA1_CreateAlgorithm
  72: 
  73: //*********************************************************
  74: // SHA1_DestroyAlgorithm
  75: //*********************************************************
  76: bool
  77: SHA1_DestroyAlgorithm
  78: 	(
  79: 		SHA1_CTX *context
  80: 	)
  81: {
  82: 	// パラメタの仮定
  83: 	ASSERT( SHA1_IsValidContext( context ) );
  84: 
  85: 	DBG_UNLOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
  86: 	free( context );
  87: 
  88: 	return true;
  89: }//SHA1_DestroyAlgorithm
  90: 
  91: //*********************************************************
  92: // SHA1_InitData
  93: //*********************************************************
  94: bool
  95: SHA1_InitData
  96: 	(
  97: 		SHA1_CTX *context
  98: 	)
  99: {
 100: 	// パラメタの仮定
 101: 	ASSERT( IsValidPtr( context, sizeof( *context ) ) );
 102: 
 103: 	context->index       = 0;
 104: 	context->length[ 0 ] = 0;
 105: 	context->length[ 1 ] = 0;
 106: 	
 107: 	const DWORD state[] =
 108: 		{
 109: 			0x67452301,
 110: 			0xefcdab89, 
 111: 			0x98badcfe, 
 112: 			0x10325476, 
 113: 			0xc3d2e1f0, 
 114: 		};
 115: 	memzero( context->buffer, sizeof( context->buffer ) );
 116: 	memcpy( context->state, state, sizeof( context->state ) );
 117: 
 118: 	ASSERT( SHA1_IsValidContext( context ) );
 119: 	return true;
 120: }//SHA1_InitData
 121: 
 122: //*********************************************************
 123: // SHA1_AddData
 124: //*********************************************************
 125: bool
 126: SHA1_AddData
 127: 	(
 128: 		      SHA1_CTX *context,
 129: 		const void     *data,
 130: 		      int       length
 131: 	)
 132: {
 133: 	// パラメタの仮定
 134: 	ASSERT( SHA1_IsValidContext( context ) );
 135: 	ASSERT( 0 < length );
 136: 	ASSERT( IsValidReadPtr( data, length ) );
 137: 
 138: 	const BYTE *src = (BYTE *)data;
 139: 	
 140: 	if ( context->index + length < sizeof( context->buffer ) )
 141: 	{
 142: 		memcpy( context->buffer + context->index, src, length );
 143: 		context->index += length;
 144: 		add_count( context, length );
 145: 	}
 146: 	else
 147: 	{	
 148: 		const int num = (sizeof( context->buffer ) - context->index);
 149: 		memcpy( context->buffer + context->index, src, num );
 150: 		add_count( context, num );
 151: 		length -= num;
 152: 		src    += num;
 153: 		SHA1_Transform( context, context->buffer, sizeof( context->buffer ) );
 154: 		context->index = 0;
 155: 
 156: 		while( sizeof( context->buffer ) <= length )
 157: 		{
 158: 			memcpy( context->buffer, src, sizeof( context->buffer ) );
 159: 			add_count( context, sizeof( context->buffer ) );
 160: 			length -= sizeof( context->buffer );
 161: 			src    += sizeof( context->buffer );
 162: 
 163: 			SHA1_Transform( context, context->buffer, sizeof( context->buffer ) );
 164: 		}
 165: 
 166: 		memcpy( context->buffer, src, length );
 167: 		context->index = length;
 168: 		add_count( context, length );
 169: 	}
 170: 	return true;
 171: }//SHA1_AddData
 172: 
 173: //*********************************************************
 174: // SHA1_GetHash
 175: //*********************************************************
 176: bool
 177: SHA1_GetHash
 178: 	(
 179: 		const SHA1_CTX *context,
 180: 		      void     *digest,
 181: 		      int       bufsize 
 182: 	)
 183: {
 184: 	// パラメタの仮定
 185: 	ASSERT( SHA1_IsValidContext( context ) );
 186: 	ASSERT( 0 < bufsize );
 187: 	ASSERT( IsValidPtr( digest, bufsize ) );
 188: 	DESTROY_BUFFER( digest, bufsize );
 189: 
 190: 	SHA1_CTX tmp_context;
 191: 	memcpy( &tmp_context, context, sizeof( *context ) );
 192: 	context = null;
 193: 
 194: 	//
 195: 	{
 196: 		int nread = tmp_context.index;
 197: 
 198: 		// 埋め
 199: 		BYTE buf[ 2 * sizeof( context->buffer ) ];
 200: 		memcpy( buf, tmp_context.buffer, nread );
 201: 		buf[ nread++ ] = 0x80; // 終端記号?
 202: 		const int padlen = (( 56 < nread ) ? 120 : 56);
 203: 		if ( 0 < (padlen - nread) )
 204: 		{
 205: 			memzero( buf + nread, padlen - nread );
 206: 		}
 207: 		memcpy( buf + padlen, tmp_context.length, 8 );
 208: 		byte_reverse( (DWORD *)(buf + padlen), 8 );
 209: 
 210: 		SHA1_AddData( &tmp_context, buf + tmp_context.index, padlen + 8 - tmp_context.index );
 211: 		ASSERT( 0 == tmp_context.index );
 212: 	}
 213: 
 214: 	//
 215: 	memcpy( digest, tmp_context.state, min( bufsize, SHA1_HASHSIZE ) );
 216: 
 217: 	return true;
 218: }//SHA1_GetHash
 219: 
 220: //*********************************************************
 221: // SHA1_GetHashText
 222: //*********************************************************
 223: bool
 224: SHA1_GetHashText
 225: 	(
 226: 		const SHA1_CTX *context,
 227: 		      char     *buffer,
 228: 			  int       bufsize
 229: 	)
 230: {
 231: 	// パラメタの仮定
 232: 	ASSERT( SHA1_IsValidContext( context ) );
 233: 	ASSERT( 0 < bufsize );
 234: 	ASSERT( IsValidStringBufferPtr( buffer, bufsize ) );
 235: 	DESTROY_TEXT_BUFFER( buffer, bufsize );
 236: 
 237: 	//
 238: 	BYTE sha1[ SHA1_HASHSIZE ];
 239: 	VERIFY( SHA1_GetHash( context, sha1, sizeof( sha1 ) ) );
 240: 
 241: 	//
 242: 	char text[ 1+SHA1_HASHTEXTSIZE ];
 243: 	{
 244: 		char *p = text;
 245: 		{for( int i = 0; i < numof( sha1 ); i += sizeof( DWORD ) )
 246: 		{
 247: 			{for( int j = 0; j < sizeof( DWORD ); ++j )
 248: 			{
 249: 				snprintf( p, numof(text) - (p - text), "%02x", sha1[ i + (sizeof( DWORD ) - j - 1) ] );
 250: 				p = strtail( p );
 251: 			}}
 252: 		}}
 253: 	}
 254: 
 255: 	//
 256: 	memcpy( buffer, text, min( bufsize, sizeof( text ) ) );
 257: 	buffer[ min( bufsize, numof( text ) ) - 1 ] = '\0';
 258: 
 259: 	return true;
 260: }//SHA1_GetHashText
 261: 
 262: 
 263: //******************************************************************************************************************
 264: //
 265: //******************************************************************************************************************
 266: //
 267: static const DWORD K0 = 0x5a827999;
 268: static const DWORD K1 = 0x6ed9eba1;
 269: static const DWORD K2 = 0x8f1bbcdc;
 270: static const DWORD K3 = 0xca62c1d6;
 271: 
 272: // basic SHA1 functions.
 273: static inline DWORD f0( DWORD x, DWORD y, DWORD z ){ return (z ^ (x & (y ^ z))); }
 274: static inline DWORD f1( DWORD x, DWORD y, DWORD z ){ return (x ^ y ^ z); }
 275: static inline DWORD f2( DWORD x, DWORD y, DWORD z ){ return ((x & y) | (z & (x | y))); }
 276: static inline DWORD f3( DWORD x, DWORD y, DWORD z ){ return (x ^ y ^ z); }
 277: 
 278: // ROTATE_LEFT rotates x left n bits.
 279: static inline DWORD ROTATE_LEFT( DWORD x, DWORD n ){ return (x << n) | (x >> ((CHAR_BIT * sizeof(DWORD)) - n)); }
 280: 
 281: static inline void r1
 282: 	(
 283: 		DWORD *A,
 284: 		DWORD  T
 285: 	)
 286: {
 287: 	A[4] = A[3];
 288: 	A[3] = A[2];
 289: 	A[2] = ROTATE_LEFT( A[1], 30 );
 290: 	A[1] = A[0];
 291: 	A[0] = T;
 292: }
 293: 
 294: static inline void r0
 295: 	(
 296: 		DWORD (*proc)(DWORD, DWORD, DWORD),
 297: 		DWORD  K,
 298: 		DWORD  P,
 299: 		DWORD *A
 300: 	)
 301: {
 302: 	const DWORD temp = ROTATE_LEFT( A[0], 5 ) + proc( A[1], A[2], A[3] ) + A[4] + P + K;
 303: 	r1( A, temp );
 304: /*	A[4] = A[3];
 305: 	A[3] = A[2];
 306: 	A[2] = ROTATE_LEFT( A[1], 30 );
 307: 	A[1] = A[0];
 308: 	A[0] = temp; */
 309: }
 310: 
 311: //*********************************************************
 312: // SHA1_IsValidContext
 313: //*********************************************************
 314: static
 315: bool
 316: SHA1_IsValidContext
 317: 	(
 318: 		const SHA1_CTX *context
 319: 	)
 320: {
 321: 	VALID_TEST( context );
 322: 	VALID_TEST( IsValidReadPtr( context, sizeof( *context ) ) );
 323: 	VALID_TEST( (0 <= context->index) && (context->index < sizeof( context->buffer ) ) );
 324: 
 325: 	return true;
 326: }//SHA1_IsValidContext
 327: 
 328: //*********************************************************
 329: // SHA1_Transform
 330: //*********************************************************
 331: static
 332: void
 333: SHA1_Transform
 334: 	(
 335: 		      SHA1_CTX  *context, 
 336: 		const BYTE      *src,
 337: 		      int        length
 338: 	)
 339: {
 340: 	ASSERT( 64 == length );
 341: 
 342: 	DWORD work[ 5 ];
 343: 	memcpy( work, context->state, sizeof( work ) );
 344: 	
 345: 	DWORD W[  80 ];
 346: 	memcpy( W, src, length );
 347: 	byte_reverse( W, length );
 348: 	{for( int i = 0; i < 16; ++i )
 349: 	{
 350: 		r0( f0, K0, W[ i ], work );
 351: 	}}
 352: 
 353: 	const struct
 354: 		{
 355: 			int   num;
 356: 			DWORD (*proc)(DWORD, DWORD, DWORD);
 357: 			DWORD key;
 358: 		} 
 359: 	step[] =
 360: 		{
 361: 			{  4, f0, K0 },
 362: 			{ 20, f1, K1 },
 363: 			{ 20, f2, K2 },
 364: 			{ 20, f3, K3 },
 365: 		};
 366: 	DWORD *p0 = &W[ 16 ];
 367: 	DWORD *p1 = &W[ 13 ];
 368: 	DWORD *p2 = &W[  8 ];
 369: 	DWORD *p3 = &W[  2 ];
 370: 	DWORD *p4 = &W[  0 ];
 371: 	{for( int i = 0; i < numof( step ); ++i )
 372: 	{
 373: 		{for( int j = 0; j < step[ i ].num; ++j )
 374: 		{
 375: 			*p0 = ROTATE_LEFT( (*p1 ^ *p2 ^ *p3 ^ *p4), 1 );
 376: 			r0( step[ i ].proc, step[ i ].key, *p0, work );
 377: 			p0++; p1++; p2++; p3++; p4++;
 378: 		}}
 379: 	}}
 380: 
 381: 	//
 382: 	{for( int i = 0; i < 5; ++i )
 383: 	{
 384: 		context->state[ i ] += work[ i ]; 
 385: 	}}
 386: }//SHA1_Transform
 387: 
 388: 
 389: //******************************************************************************************************************
 390: //
 391: //******************************************************************************************************************
 392: //*********************************************************
 393: // 
 394: //*********************************************************
 395: static
 396: void
 397: add_count
 398: 	(
 399: 		SHA1_CTX  *context, 
 400: 		int        nread
 401: 	)
 402: {
 403: 	const int nbits = nread << 3;
 404: 	context->length[ 1 ] += nbits;
 405: 
 406: 	if ( context->length[ 1 ] < (unsigned int)nbits )
 407: 	{
 408: 		UNREACHCODE( "SHA1 overflow" );
 409: 		context->length[ 0 ]++;              
 410: 	}
 411: }//add_count
 412: 
 413: //*********************************************************
 414: // byte_reverse
 415: //*********************************************************
 416: static
 417: void
 418: byte_reverse
 419: 	(
 420: 		DWORD *buffer,
 421: 		int    count
 422: 	)
 423: {
 424: 	count /= sizeof( DWORD );
 425: 	{for( int i = 0; i < count; ++i )
 426: 	{
 427: 		const DWORD value = ( buffer[ i ] << 16 ) | ( buffer[ i ] >> 16 );
 428: 		buffer[ i ] = ( ( value & 0xFF00FF00L ) >> 8 ) | ( ( value & 0x00FF00FFL ) << 8 );
 429: 	}}
 430: 
 431: }//byte_reverse
 432: 
 433: 
 434: //******************************************************************************************************************
 435: // TEST
 436: //******************************************************************************************************************
 437: 
 438: 
 439: #ifdef _DEBUG // デバッグ時のみ
 440: 
 441: 
 442: //*********************************************************
 443: // test_sha1
 444: //*********************************************************
 445: DEFINE_TESTPROC( test_sha1 )
 446: {
 447: 	//---------------------------------------------------------
 448: 	// 定数 の テスト
 449: 	//---------------------------------------------------------
 450: 
 451: 	//---------------------------------------------------------
 452: 	// ファイルスコープ関数 の テスト
 453: 	//---------------------------------------------------------
 454: 
 455: 	//---------------------------------------------------------
 456: 	// 公開関数 の テスト
 457: 	//---------------------------------------------------------
 458: 
 459: 	static const BYTE testcase[] = 
 460: 		{
 461: 			0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 
 462: 			0xdd, 0x6a, 0xc7, 0x8e, 0xb3, 0x98, 0xf3, 0xde, 0x0b, 0x9d, 0xa8, 0xcd, 0xc5, 0xa9, 0x72, 0xca, 0x43, 0x78, 0x5b, 0xd8, 
 463: 			0x80, 0x26, 0x77, 0x90, 0x53, 0x35, 0x91, 0x83, 0x77, 0x22, 0xdd, 0x79, 0x30, 0x41, 0xb1, 0x89, 0xd6, 0x5c, 0xe7, 0x8b, 
 464: 			0xa1, 0xab, 0xd5, 0x78, 0x73, 0xb3, 0xb0, 0x30, 0x72, 0xef, 0x42, 0x97, 0xc2, 0xf6, 0x9b, 0x39, 0x1d, 0x50, 0x70, 0x82, 
 465: 			0xd0, 0x31, 0x92, 0x0b, 0x81, 0xe2, 0xc3, 0x56, 0x5b, 0x28, 0xd0, 0xd6, 0xa9, 0x6c, 0x66, 0x36, 0xcf, 0x60, 0x39, 0x64, 
 466: 			0x4f, 0x87, 0x02, 0x37, 0xd8, 0xc0, 0x8b, 0x5d, 0x14, 0xa2, 0x88, 0x8a, 0xdf, 0x81, 0x4f, 0x38, 0xde, 0xc1, 0x63, 0x68, 
 467: 			0x5a, 0xa7, 0x90, 0x71, 0xe5, 0xe3, 0xeb, 0x53, 0x5a, 0x61, 0x41, 0x1b, 0xa5, 0x53, 0x17, 0xe7, 0x7d, 0x25, 0x0b, 0xd7, 
 468: 			0xc0, 0x8d, 0x8f, 0x08, 0x1b, 0x13, 0xb5, 0xf3, 0x2d, 0x9a, 0x71, 0xc4, 0x2e, 0x85, 0x1f, 0x26, 0x04, 0x6d, 0xd1, 0xe9, 
 469: 			0xa3, 0xd1, 0x27, 0x73, 0x44, 0xc9, 0xa4, 0x3e, 0x7e, 0xd3, 0xad, 0xed, 0x4f, 0xed, 0x3f, 0x67, 0xcd, 0x7b, 0xb8, 0xc8, 
 470: 			0x07, 0xb5, 0x63, 0x7b, 0x3b, 0x49, 0xda, 0xf8, 0x2f, 0x5c, 0xf8, 0xa5, 0x0c, 0xbf, 0x54, 0x46, 0xc2, 0xa4, 0x79, 0xd8, 
 471: 			0xaf, 0x92, 0xab, 0x4d, 0x4b, 0x17, 0x8e, 0x37, 0x23, 0x7a, 0xec, 0x1d, 0x42, 0x67, 0xb7, 0x27, 0xea, 0xea, 0x39, 0xca, 
 472: 			0x88, 0x64, 0x03, 0xa2, 0xe7, 0x68, 0xe2, 0xc2, 0x78, 0xf7, 0x99, 0xdf, 0xbc, 0x9a, 0xc1, 0x24, 0x21, 0x28, 0xcf, 0xd5, 
 473: 			0xdd, 0x1b, 0x25, 0x88, 0x68, 0x6b, 0xed, 0x55, 0x3c, 0xa1, 0x8a, 0x8e, 0x5e, 0x18, 0x19, 0x39, 0x69, 0x4d, 0x72, 0x1f, 
 474: 			0xb0, 0x70, 0x91, 0xcb, 0x5a, 0x4c, 0x8b, 0x77, 0x28, 0xcc, 0x46, 0x63, 0x5d, 0x13, 0xc8, 0x4e, 0x52, 0xc9, 0x6e, 0x4c, 
 475: 			0x75, 0xe0, 0x65, 0xbf, 0xbf, 0x47, 0xce, 0x50, 0xbc, 0x9c, 0xc6, 0xaf, 0xb0, 0x35, 0xe8, 0x34, 0xd0, 0x81, 0xd9, 0x0a, 
 476: 			0x15, 0xf2, 0x55, 0x77, 0xa8, 0xa3, 0xdb, 0x04, 0x71, 0x74, 0x13, 0x63, 0xaa, 0xcf, 0xe5, 0x3b, 0x97, 0x85, 0xd8, 0xc0, 
 477: 			0x3c, 0xce, 0x9e, 0xb9, 0x61, 0x28, 0x69, 0xe5, 0xf0, 0x7b, 0xc1, 0x13, 0xa5, 0x49, 0x29, 0xf0, 0xae, 0xfc, 0x8b, 0xab, 
 478: 			0x1b, 0x10, 0xe6, 0xc8, 0xe3, 0xcd, 0x6b, 0x51, 0x31, 0xae, 0x48, 0xe9, 0x0c, 0x40, 0xaa, 0x10, 0x6b, 0xd6, 0x81, 0x65, 
 479: 			0x26, 0xc1, 0x29, 0xf0, 0x82, 0x30, 0x6b, 0xc1, 0x63, 0x24, 0x49, 0xc7, 0x29, 0x62, 0xf9, 0x8b, 0x55, 0xb1, 0xf0, 0xc5, 
 480: 			0xa3, 0x45, 0x7a, 0xea, 0x55, 0x59, 0x60, 0xa9, 0x03, 0x12, 0x20, 0xf3, 0x97, 0x38, 0xc2, 0xbe, 0x0d, 0xc2, 0xc8, 0xa2, 
 481: 			0xd8, 0xf2, 0xd3, 0xf2, 0x65, 0x2e, 0x28, 0xe4, 0xbc, 0xae, 0x66, 0x59, 0xb7, 0xa4, 0x38, 0xb8, 0xe1, 0x0b, 0x6c, 0x54, 
 482: 			0x1d, 0xd1, 0xef, 0x1b, 0xb7, 0x9e, 0x68, 0xc5, 0x6a, 0x7e, 0x5b, 0x1c, 0x37, 0xa6, 0x61, 0xfb, 0xb4, 0x64, 0x3a, 0x4d, 
 483: 			0x80, 0xd8, 0xdf, 0xcb, 0x3c, 0x7d, 0x70, 0x8a, 0x91, 0xbb, 0x55, 0x66, 0x10, 0x14, 0xef, 0x77, 0xd2, 0x65, 0x4a, 0x20, 
 484: 			0x11, 0x97, 0x69, 0xf1, 0xb5, 0x1b, 0xc3, 0x59, 0xef, 0xfe, 0xf9, 0x89, 0x42, 0x4e, 0x59, 0x66, 0x5e, 0xef, 0xcb, 0x3b, 
 485: 			0x7f, 0xa0, 0x54, 0x47, 0x34, 0x31, 0x15, 0xa9, 0x85, 0x71, 0xd2, 0x5c, 0xc4, 0x4c, 0x49, 0xe9, 0xd5, 0x17, 0xdb, 0x5f, 
 486: 			0x38, 0xb5, 0x26, 0x44, 0xbb, 0xd5, 0x8c, 0xe2, 0x05, 0x9b, 0x7c, 0x5c, 0x59, 0x22, 0xea, 0xda, 0x80, 0xce, 0xb0, 0xd0, 
 487: 			0x62, 0x22, 0xa1, 0xf0, 0x6b, 0x69, 0xfd, 0x75, 0x9c, 0x13, 0xb6, 0x2c, 0x4a, 0x75, 0x65, 0xa4, 0x47, 0x1f, 0xe0, 0x07, 
 488: 			0x42, 0x36, 0xc6, 0x34, 0xec, 0x62, 0x72, 0xb3, 0xea, 0x02, 0x3d, 0x9d, 0x95, 0x38, 0x89, 0xb7, 0x1d, 0xf4, 0xd2, 0xa3, 
 489: 			0xde, 0xa4, 0x78, 0x77, 0x11, 0x4e, 0x03, 0xb0, 0xce, 0xca, 0xd1, 0xb4, 0x7e, 0x5d, 0x7d, 0x1b, 0x8d, 0xae, 0x00, 0x52, 
 490: 			0x5f, 0x76, 0x09, 0x69, 0x3c, 0xe0, 0x49, 0x03, 0x40, 0xe7, 0x89, 0xc9, 0x75, 0x2b, 0x7d, 0xcb, 0xb2, 0x91, 0xc1, 0xef, 
 491: 			0xca, 0xca, 0x73, 0xaf, 0x36, 0x93, 0x98, 0x20, 0xdf, 0x2a, 0x86, 0x52, 0x28, 0x69, 0x3b, 0xbc, 0x83, 0xe5, 0x92, 0xd4, 
 492: 			0x8b, 0x62, 0xfc, 0x19, 0xd2, 0x8e, 0x6c, 0x85, 0x5a, 0xdc, 0x52, 0x6a, 0x4e, 0xd0, 0x23, 0xe0, 0xa6, 0x0a, 0xaa, 0xa2, 
 493: 		};
 494: 
 495: 	// SHA1
 496: 	SHA1_CTX *context = SHA1_CreateAlgorithm();
 497: 
 498: 	// 一気にハッシュ
 499: 	{for( int i = 1; (i * SHA1_HASHSIZE) < numof( testcase ); ++i )
 500: 	{
 501: 		SHA1_InitData( context );
 502: 		SHA1_AddData( context, testcase, i * SHA1_HASHSIZE );
 503: 
 504: 		BYTE sha[ SHA1_HASHSIZE ];
 505: 		{for( int j = 1; j < numof( sha ); ++j )
 506: 		{
 507: 			SHA1_GetHash( context, sha, j );
 508: 			VERIFY( 0 == memcmp( sha, testcase + (i * SHA1_HASHSIZE), j ) );
 509: 		}}
 510: 	}}
 511: 
 512: 	// 徐々にハッシュ
 513: 	SHA1_InitData( context );
 514: 	{for( int i = 1; (i * SHA1_HASHSIZE) < numof( testcase ); ++i )
 515: 	{
 516: 		{for( int j = 0; j < SHA1_HASHSIZE; ++j )
 517: 		{
 518: 			SHA1_AddData( context, &testcase[ j + ((i-1) * SHA1_HASHSIZE) ], 1 );
 519: 		}}
 520: 
 521: 		BYTE sha[ SHA1_HASHSIZE ];
 522: 		SHA1_GetHash( context, sha, numof( sha ) );
 523: 		VERIFY( 0 == memcmp( sha, testcase + (i * SHA1_HASHSIZE), numof( sha ) ) );
 524: 	}}
 525: 
 526: 	VERIFY( SHA1_DestroyAlgorithm( context ) );
 527: 
 528: }//test_sha1
 529: 
 530: 
 531: #endif // #ifdef _DEBUG
 532: 
 533: 
 534: //** end **
 535: 

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > tools > misc > sha1.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/tools/misc/sha1_cpp.shtml
>> Amazon.co.jp 『たまゆら童子』 へ
>> 楽天ブックス 『たまゆら童子』 へ